<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-button
          icon="Refresh"
          style="margin: 3px 3px"
          size="small"
          type="primary"
          @click="refresh"
          v-hasPermi="['monitor:cal:query']"
      >刷新
      </el-button>
      <el-button
          :disabled="single"
          style="margin: 3px 3px"
          icon="Download"
          size="small"
          type="success"
          @click="restart"
          v-hasPermi="['monitor:cal:restart']"
      >重启
      </el-button>
    </el-row>
    <xTable ref="serveRef" v-loading="loading" :data="serveList" multiple="false"
            refId="serveRef" @selection-change="handleSelectionChange">
      <template #columList>
        <el-table-column label="序号" type="index" width="40"/>
        <el-table-column align="center" label="IP地址" prop="address" width="500"/>
        <el-table-column align="left" label="状态" prop="status">
          <template #default="scope" width="200">
            <span>{{ scope.row.status == 1 ? "无此计算服务" : (scope.row.status == 0 ? "正常" : "异常") }}</span>
          </template>
        </el-table-column>
      </template>
    </xTable>
  </div>
</template>

<script setup name="ServeInfo">
import {getServeStatus, restartServe} from '@/api/monitor/serveInfo';

const {proxy} = getCurrentInstance();
const addresses = ref([]);
const ids = ref([]);
const rows = ref([]);
const single = ref(true);
const serveList = ref([]);
const loading = ref(true);
const total = ref(0);
const address = ref();
const data = reactive({
  queryParams: {
    address: null,
  }
});

const {queryParams} = toRefs(data);

function getServe() {
  loading.value = true;
  getServeStatus().then(response => {
    serveList.value = response;
    loading.value = false;
  });
}

/** 重启按钮操作 */
function restart() {
  restartServe(queryParams.value).then(response => {
    if (response.success) {
      proxy.$modal.msgSuccess("重启成功");
    } else {
      proxy.$modal.msgError(response.msg);
    }
  });
}

function handleSelectionChange(selection) {
  if (selection.length == 0) {
    ids.value = [];
    addresses.value = [];
  } else {
    addresses.value = selection.map(item => item.address);
    rows.value = selection.map(item => item);
  }
  single.value = selection.length != 1;
  if (selection.length == 1) {
    queryParams.value.address = addresses.value[0];
    single.value = false;
    return;
  }
  if (selection.length != 1) {
    single.value = true;
  }
}

/** 重置按钮操作 */
function refresh() {
  getServe();
}

getServe();
</script>
